package com.the4man.framework.service.impl;

import com.the4man.framework.cache.PermissionCache;
import com.the4man.framework.dao.RolePermissionMapMapper;
import com.the4man.framework.dao.SystemManagerMapper;
import com.the4man.framework.model.*;
import com.the4man.framework.pagemodel.PSystemManager;
import com.the4man.framework.service.SystemManagerServiceI;
import com.the4man.framework.util.EncryptUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
@Service("systemManagerService")
public class SystemManagerServiceImpl implements SystemManagerServiceI {
    @Autowired
    private SystemManagerMapper pSystemManagerMapper;
    @Autowired
    private RolePermissionMapMapper rolePermissionMapMapper;
    @Autowired
    private PermissionCache permissionCache;

    public SystemManagerServiceImpl() {
    }

    public boolean saveUserDetail(PSystemManager param) throws Exception {
//        int flag = false;
        if(param.getUsernewpwd() != null) {
            param.setUsernewpwd(EncryptUtil.getInstance().encryptMD5(param.getUsernewpwd()));
        }

        int flag = this.pSystemManagerMapper.saveUserDetail(param);
        return flag > 0;
    }

    public User getUserInfo(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.getUserInfo(param);
    }

    public List<Role> getEtlRoleData(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.getEtlRoleData(param);
    }

    public boolean addRoleData(PSystemManager param) throws Exception {
//        int flag = false;
        int flag = this.pSystemManagerMapper.addRoleData(param);
        return flag > 0;
    }

    public boolean deleteBatchRole(List<String> delList) throws Exception {
//        int flag = false;
        this.pSystemManagerMapper.deleteBatchRoleMoudle(delList);
        int flag = this.pSystemManagerMapper.deleteBatchRole(delList);
        return flag > 0;
    }

    public boolean modifyRoleData(PSystemManager param) throws Exception {
//        int flag = false;
        int flag = this.pSystemManagerMapper.modifyRoleData(param);
        return flag > 0;
    }

    public boolean checkRoleName(PSystemManager model) throws Exception {
        return this.pSystemManagerMapper.checkRoleName(model) >= 1;
    }

    public List<User> getAllUserInfo(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.getAllUserInfo(param);
    }

    public boolean addUserInfo(PSystemManager param) throws Exception {
//        int flag = false;
        if(param.getUserpwd() != null) {
            param.setUserpwd(EncryptUtil.getInstance().encryptMD5(param.getUserpwd()));
        }

        param.setLoginType("DQM");
        int flag = this.pSystemManagerMapper.addUserInfo(param);
        return flag > 0;
    }

    public boolean deleteBatchUser(List<String> delList) throws Exception {
//        int flag = false;
        int flag = this.pSystemManagerMapper.deleteBatchUser(delList);
        return flag > 0;
    }

    public boolean modifyUserInfo(PSystemManager param) throws Exception {
//        int flag = false;
        if(param.getUserpwd() != null) {
            param.setUserpwd(EncryptUtil.getInstance().encryptMD5(param.getUserpwd()));
        }

        int flag = this.pSystemManagerMapper.modifyUserInfo(param);
        return flag > 0;
    }

    public List<Moudle> getEtlMoudleData(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.getEtlMoudleData(param);
    }

    public boolean addMoudleData(PSystemManager param) throws Exception {
//        int flag = false;
        int flag = this.pSystemManagerMapper.addMoudleData(param);
        return flag > 0;
    }

    public boolean deleteBatchMoudle(List<String> delList) throws Exception {
//        int flag = false;
        int flag = this.pSystemManagerMapper.deleteBatchMoudle(delList);
        return flag > 0;
    }

    public boolean modifyMoudleData(PSystemManager param) throws Exception {
//        int flag = false;
        int flag = this.pSystemManagerMapper.modifyMoudleData(param);
        return flag > 0;
    }

    public boolean checkMoudleName(PSystemManager model) throws Exception {
        return this.pSystemManagerMapper.checkMoudleName(model) >= 1;
    }

    public List<Moudle> getMoudleDataByRole(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.getMoudleDataByRole(param);
    }

    public int insertLimitRoleModule(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.insertLimitRoleModule(param);
    }

    public int deleteCurrentRoleModule(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.deleteCurrentRoleModule(param);
    }

    public List<Department> getEtlDepartmentData(PSystemManager param) throws Exception {
        List<Department> list = this.pSystemManagerMapper.getEtlDepartmentData(param);
        List<Department> listData = new ArrayList();
        Department dept;
        Iterator var5;
        if(param.getStatus().equals("0")) {
            var5 = list.iterator();

            while(var5.hasNext()) {
                dept = (Department)var5.next();
                dept.setId(dept.getDeptId());
                dept.setText(dept.getDeptName());
                dept.setPid(dept.getPdeptId());
                listData.add(dept);
            }
        } else {
            for(var5 = list.iterator(); var5.hasNext(); listData.add(dept)) {
                dept = (Department)var5.next();
                dept.setId(dept.getDeptId());
                dept.setText(dept.getDeptName());
                if(StringUtils.isEmpty(dept.getPdeptId())) {
                    dept.setPid("parent");
                } else {
                    dept.setPid(dept.getPdeptId());
                }
            }
        }

        return listData;
    }

    public boolean addDepartment(PSystemManager param) throws Exception {
//        int insertFlag = false;
        int insertFlag = this.pSystemManagerMapper.addDepartment(param);
        return insertFlag > 0;
    }

    public boolean deleteDepartment(PSystemManager param) throws Exception {
//        int insertFlag = false;
        int insertFlag = this.pSystemManagerMapper.deleteDepartment(param);
        return insertFlag > 0;
    }

    public List<Department> modifyDepartment(PSystemManager param) throws Exception {
        return this.pSystemManagerMapper.getEtlDepartmentData(param);
    }

    public boolean modifyDepartmentData(PSystemManager param) throws Exception {
//        int insertFlag = false;
        int insertFlag = this.pSystemManagerMapper.modifyDepartmentData(param);
        return insertFlag > 0;
    }

    public void assignPermission(String roleId, String permissionIds) {
        if(roleId == null) {
            throw new IllegalArgumentException("角色编号不能为空");
        } else {
            this.rolePermissionMapMapper.deleteByRoleId(roleId);
            if(org.apache.commons.lang.StringUtils.isNotBlank(permissionIds)) {
                String[] pids = org.apache.commons.lang.StringUtils.split(permissionIds, ";");
                HashSet<String> permissionIdsSet = new HashSet();
                CollectionUtils.addAll(permissionIdsSet, pids);
                Iterator var6 = permissionIdsSet.iterator();

                while(var6.hasNext()) {
                    String pid = (String)var6.next();
                    if(!org.apache.commons.lang.StringUtils.isBlank(pid)) {
                        RolePermissionMap rolePermissionMap = new RolePermissionMap(pid, roleId);
                        this.rolePermissionMapMapper.insert(rolePermissionMap);
                    }
                }
            }

            this.permissionCache.refreshByRoleId(roleId);
        }
    }

    public List<RolePermissionMap> selectByRoleId(String roleId) {
        return this.rolePermissionMapMapper.selectByRoleId(roleId);
    }

    public List<RolePermissionMap> findAll() {
        return this.rolePermissionMapMapper.findAll();
    }

    public void insert(RolePermissionMap rolePermissionMap) {
        this.rolePermissionMapMapper.insert(rolePermissionMap);
    }

    public void deleteByRoleId(String roleId) {
        this.rolePermissionMapMapper.deleteByRoleId(roleId);
    }
}
